<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">
    <#--获取主键字段-->
    <#list table.fields as field>
        <#if field.keyFlag>
            <#assign primaryKey = "${field.name}">
        </#if>
    </#list>

    <sql id="AllColumn">
        ${table.fieldNames}
    </sql>

    <select id="getPaged${cfg.entityName}List" resultType="${package.Entity}.${entity}">
        select
        <include refid="AllColumn"/>
        from ${table.name}
        <if test="${cfg.searchField}!=null and ${cfg.searchField}!=''">
        where ${cfg.searchField} like CONCAT(${r'#{'}${cfg.searchField}},'%')
        </if>
    </select>

    <select id="get${cfg.entityName}ById" resultType="${package.Entity}.${entity}">
        select
        <include refid="AllColumn"/>
        from ${table.name}
        where ${primaryKey}= ${r'#{'}${primaryKey}}
    </select>

    <insert id="add${cfg.entityName}" parameterType="${package.Entity}.${entity}">
        insert into ${table.name} (${table.fieldNames})
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list table.fieldNames?split(", ") as fieldName>
                ${r'#{'}${fieldName}},
            </#list>
        </trim>
    </insert>

    <update id="edit${cfg.entityName}" parameterType="${package.Entity}.${entity}">
        update ${table.name}
        <trim prefix="set" suffixOverrides=",">
            <#list table.fields as field>
                <#if field.name!=primaryKey && field.name!="createTime">
                ${field.name} = ${r'#{'}${field.name}},
                </#if>
            </#list>
        </trim>
        where ${primaryKey} = ${r'#{'}${primaryKey}}
    </update>

    <delete id="delete${cfg.entityName}ByIds">
        delete from ${table.name}
        where ${primaryKey} in
        <foreach item="${primaryKey}" index="index" collection="list" open="(" separator="," close=")">
            ${r'#{'}${primaryKey}}
        </foreach>
    </delete>

</mapper>
